define(['angular',"text!www/trends/community_new.html",'url','common','swiper','jqload'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($timeout,$scope,$rootScope,$http){
	   hideMenu();
	   initFooter(0);
	   showSpin();
	   $rootScope.notify = cl.user.notify;
	   //console.log(cl.user.notify);
	  var community = function(){
		    this.banners = [];
			this.articleCat = [];//分类
			this.articles = [];//文章详情
			this.page = 1;
			this.catId = "ALL";
			this.catAllActive = "active";
			this.search = "";
			this.baseUrl = ip+"/";
			this.base = ip;
			this.adUrl = ip+"/"+"data/afficheimg/";
			this.busy = false;
			this.reSet = false;
			this.wxid = 0;
	   };
	   $scope.community = new community();
	   $scope.community.initCommunity = function(){//请求全部
	    	 if (this.busy){
	        	 hideSpin();
	        	 return;
	         }
	         this.busy = true;
	         //将数据清空
	         var pro = this;
	         var url = cl.getCommunityNewUrl+"&callback=JSON_CALLBACK&catId="+this.catId+"&search="+this.search ;
	         $http.jsonp(url).success(function (res) {
		        	hideSpin();
					if(res.code=="CL02045"){//获取成功
						
						cl.user.notify = res.result.notify;
						
						$rootScope.notify = cl.user.notify ;
						
						pro.banners = res.result.ad;
						pro.articleCat = res.result.articleCat;
						pro.articleCa = res.result.articleCa;
						
						pro.articles = res.result.articles;
						if(pro.articles.length>=0){
							pro.page += 1;
						}
					}
					
					
					
					$timeout(function(){
						initBox();lazyloadImage();btnScroll();
					},1000);
	         
					pro.busy = false;
	         });
	     };
	 	$scope.community.changeCat = function(catId,index){
	 		showSpin();
			this.articles = [];
			this.catId = catId;
			this.catAllActive = "";
			for(var k in this.articleCat){
    			this.articleCat[k].active="";
	    	}
			if(index!="ALL")
			{
				this.articleCat[index].active = "active";
			}else{
				this.catAllActive = "active";
			}
			this.page = 1;
			this.reSet = true;
			this.nextPage();
	 	};
	 	$scope.community.searchArticle = function(){
	 		showSpin();
			this.articles = [];
//			for(var k in this.articleCat){
//    			this.articleCat[k].active="";
//	    	}
//			this.catAllActive = "active";
//			this.catId = "ALL";
			
			
			
			this.page = 1;
			this.reSet = true;
			this.nextPage();
	 	};
	 	
	 	$scope.community.nextPage = function(){//上拉刷新
	 		if(!isFooter()){
	 			if(!this.reSet){
	 				return ;
	 			}
	 		}
	 		if (this.busy){
	        	 hideSpin();
	        	 return;
	         }
	 		
//	 		if (this.catId == "ALL"){
//	        	 hideSpin();
//	        	 return;
//	         }
		       this.busy = true;
		       var pro = this;
		       var url = cl.refreshCommunityNewUrl+"&callback=JSON_CALLBACK&page="+this.page+"&catId="+this.catId+"&search="+this.search ;
		       
		       $http.jsonp(url).success(function (res) {
		      	//处理成功的情况
		    	   if(res.code=="CL02046"){
		    		   if(res.result.length!=0){
				             var items = res.result;
				             for (var i = 0; i < items.length; i++) {
				            	 pro.articles.push(items[i]);
				             }
				             pro.page += 1;
						}
			      	}
		    	   setTimeout("lazyloadImage();",1000);
		    	   pro.busy = false;
		    	   pro.reSet = false;
		    	   hideSpin();
		       });
	 	};
 		$scope.community.initCommunity();
    };
    return {controller:controller,tpl:tpl};
});
